package binaryOctopus;
import java.io.*;
import java.util.ArrayList;

/**
 * The Game class contains all methods that are needed to play the game of
 * checkers includes all features that come along. Meaning that it includes 
 * all standard rules and it allows the user to start a new game. It also
 * allows for saving and loading a specific game. Everything in the game
 * is displayed by a Java GUI.
 */
public class Game 
{
	//an array of pieces that represents the board
	//0 means open square, 1 means regular piece, 2 means king
	private int piecesOnBoard[][]; 
	//two players that are currently playing the gane
	private Player p1, p2;
	//boolean that determines who's turn it is
	private boolean isP1Turn;
  //output stream to create save file
  private FileOutputStream fOutS;
  //reader for save files
  private BufferedReader reader;
  
  
  //SOMEONE INCORPORATE THIS INTO THE ALGORITHMS SO IT'LL WORK WITH THE GUI!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  //SOMEONE INCORPORATE THIS INTO THE ALGORITHMS SO IT'LL WORK WITH THE GUI!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  //SOMEONE INCORPORATE THIS INTO THE ALGORITHMS SO IT'LL WORK WITH THE GUI!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  //SOMEONE INCORPORATE THIS INTO THE ALGORITHMS SO IT'LL WORK WITH THE GUI!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  //SOMEONE INCORPORATE THIS INTO THE ALGORITHMS SO IT'LL WORK WITH THE GUI!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  //private boolean isKing = false;
	
	/**
	 * Constructor that starts the game and sets the size of the board
	 * @param numRows
	 * @param numCols
	 */
	public Game(int numRows, int numCols, boolean playAI) 
	{
		//inialize board to be empty
		piecesOnBoard = new int[numRows][numCols];
		for(int i = 0; i < numRows; i++)
		{
			for(int j = 0; j < numCols; j++)
			{
				piecesOnBoard[i][j] = 0;
			}
		}
		
		//add constructor to player that will give it pieces???
		p1 = new HumanPlayer();
		
		if(playAI)
		{
			p2 = new AI();
		}
		else
		{
			p2 = new HumanPlayer();
		}
		
		isP1Turn = true;
	}
	

	/**
	 * Starts new game by setting the pieces on the board and also
	 * setting the players, thus allowing for there to be a human
	 * or AI player.
	 */
	public void newGame(boolean playAI)
	{		
		isP1Turn = true;
		
		piecesOnBoard[0][1] = 1;
		piecesOnBoard[0][3] = 1;
		piecesOnBoard[0][5] = 1;
		piecesOnBoard[0][7] = 1;
		piecesOnBoard[1][0] = 1;
		piecesOnBoard[1][2] = 1;
		piecesOnBoard[1][4] = 1;
		piecesOnBoard[1][6] = 1;
		piecesOnBoard[2][1] = 1;
		piecesOnBoard[2][3] = 1;
		piecesOnBoard[2][5] = 1;
		piecesOnBoard[2][7] = 1;
		
		piecesOnBoard[7][0] = 1;
		piecesOnBoard[7][2] = 1;
		piecesOnBoard[7][4] = 1;
		piecesOnBoard[7][6] = 1;
		piecesOnBoard[6][1] = 1;
		piecesOnBoard[6][3] = 1;
		piecesOnBoard[6][5] = 1;
		piecesOnBoard[6][7] = 1;
		piecesOnBoard[5][0] = 1;
		piecesOnBoard[5][2] = 1;
		piecesOnBoard[5][4] = 1;
		piecesOnBoard[5][6] = 1;
		
//		Coordinates[] p1Pieces = {new Coordinates(0, 1), new Coordinates(0, 3),new Coordinates(0, 5),
//				new Coordinates(0, 7), new Coordinates(1, 0), new Coordinates(1, 2), new Coordinates(1, 4),
//				new Coordinates(1, 6), new Coordinates(2, 1), new Coordinates(2, 3), new Coordinates(2, 5),
//				new Coordinates(2, 7)};
		
		ArrayList<Coordinates> p1Pieces = new ArrayList<Coordinates>();
		p1Pieces.add(new Coordinates(0, 1));
		p1Pieces.add(new Coordinates(0, 3));
		p1Pieces.add(new Coordinates(0, 5));
		p1Pieces.add(new Coordinates(0, 7));
		p1Pieces.add(new Coordinates(1, 0));
		p1Pieces.add(new Coordinates(1, 2));
		p1Pieces.add(new Coordinates(1, 4));
		p1Pieces.add(new Coordinates(1, 6));
		p1Pieces.add(new Coordinates(2, 1));
		p1Pieces.add(new Coordinates(2, 3));
		p1Pieces.add(new Coordinates(2, 5));
		p1Pieces.add(new Coordinates(2, 7));
		
		
//		Coordinates[] p2Pieces = {new Coordinates(7, 0), new Coordinates(7, 2),new Coordinates(7, 4),
//				new Coordinates(7, 6), new Coordinates(6, 1), new Coordinates(6, 3), new Coordinates(6, 5),
//				new Coordinates(6, 7), new Coordinates(5, 0), new Coordinates(5, 2), new Coordinates(5, 4),
//				new Coordinates(5, 6)};
		
		ArrayList<Coordinates> p2Pieces = new ArrayList<Coordinates>();
		p2Pieces.add(new Coordinates(7, 0));
		p2Pieces.add(new Coordinates(7, 2));
		p2Pieces.add(new Coordinates(7, 4));
		p2Pieces.add(new Coordinates(7, 6));
		p2Pieces.add(new Coordinates(6, 1));
		p2Pieces.add(new Coordinates(6, 3));
		p2Pieces.add(new Coordinates(6, 5));
		p2Pieces.add(new Coordinates(6, 7));
		p2Pieces.add(new Coordinates(5, 0));
		p2Pieces.add(new Coordinates(5, 2));
		p2Pieces.add(new Coordinates(5, 4));
		p2Pieces.add(new Coordinates(5, 6));
		
		
		p1 = new HumanPlayer(p1Pieces);
		
		if(playAI)
		{
			p2 = new AI(p2Pieces, piecesOnBoard);
		}
		else
		{
			p2 = new HumanPlayer(p2Pieces);
		}
	}
	
	public boolean king(int x, int y)
	{
		if(piecesOnBoard[y][x] == 2)
		{
			return true;
		}
		
		return false;
	}
	
	/**
	 * This method will look to see if a player has won and then
	 * increment statistics 
	 * @return if a player won
	 */
	public boolean win()
	{
		return false;
	}
	
	/**
	 * Saves the current game, including location and color of pieces, current 
	 * player's turn and game time, to a save file which will be in text format.
	 */
	public void save(){
   /**
    Path fPath = "filepath?";
    try {
      Create the empty file with default permissions, etc.
      fOutS = new FileOutputStream(fPath)
      
      new PrintStream(fOutS).println(isP1Turn);
      new PrintStream(fOutS).println(gametime); 
      
      for(int i = 0; i < 7; i++){
        for(int j = 0; j < 7; j++){
        new PrintStream(fOutS).println(pieces w/ king status, coords, alliance);
        }
      }
      
      fOutS.close();
    } 
    catch (Exception e){
    }*/
    
	}
	
	/**
	 * Loads a game back to the current state that it was saved. It must 
	 * parse it in based on the format it was saved in.
	 */
	public void load(){
    /**
    FileInputStream fInS = new FileInputStream(saveFile); 
		 DataInputStream in = new DataInputStream(fInS);
		 reader = new BufferedReader(new InputStreamReader(in));
		*/
	}
}
